package com.cong.security.app.social;

import com.cong.security.core.constant.UrlConstants;
import com.cong.security.core.social.MySpringSocialConfigurer;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 实现BeanPostProcessor接口,目的是在spring中所有的bean初始化之后将默认的浏览器配置的bean强转为APP注册的bean,以促使浏览器配置在APP模块不生效(不跳转URL)
 */
@Component
public class SpringSocialConfigurerPostProcessor implements BeanPostProcessor {

	/**
	 * 初始化前
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	/**
	 * 初始化后
	 */
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		// 当mySocialSecurityConfig初始化完成，修改signupUrl参数
		if (StringUtils.equals(beanName, "mySocialSecurityConfig")) {
			// 强转
			MySpringSocialConfigurer configurer = (MySpringSocialConfigurer) bean;
			configurer.signupUrl(UrlConstants.DEFAULT_APP_SOCIAL_SIGN_UP_URL);
			return configurer;
		}
		return bean;
	}
}
